package com.itcast.dao;

import com.itcast.pojo.Friendlist;
import com.itcast.pojo.User;
import com.itcast.utils.ConnectionFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FriendDaoImpl implements  FriendDao {
    @Override
    public List<Friendlist> findAllFriendlist(String userId) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            //获取连接
            connection= ConnectionFactory.getConnection();
            //定义sql
            String sql="select * from friendlist where user_id=? ";
            //获取集装箱
            preparedStatement=connection.prepareStatement(sql);
            //给？赋值
            preparedStatement.setString(1,userId);
            //执行sql
            resultSet=preparedStatement.executeQuery();
            List<Friendlist> friendlists=new ArrayList<>();
            //遍历取值
            while (resultSet.next())
            {
                Friendlist friendlist=new Friendlist();
                friendlist.setId(resultSet.getInt("id"));
                friendlist.setFriendId(resultSet.getString("user_id"));
                friendlist.setFriendId(resultSet.getString("friend_id"));
                 friendlists.add(friendlist);
            }
            return friendlists;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally{
            ConnectionFactory.close(resultSet,preparedStatement,connection);
        }
        return null;
    }

    @Override
    public void InsertFriend(String userId, String friendId) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            //获取连接
            connection=ConnectionFactory.getConnection();
            //定义sql
            String sql="insert into friendlist(user_id,friend_id) values (?,?)";
            //获取集装箱
            preparedStatement=connection.prepareStatement(sql);
            //给？赋值
            preparedStatement.setString(1,userId);
            preparedStatement.setString(2,friendId);
            //执行sql
            int result=preparedStatement.executeUpdate();


        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(resultSet,preparedStatement,connection);
        }
    }

    @Override
    public void DeleteFriend(String userId, String friendId) {
        
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            //获取连接
            connection=ConnectionFactory.getConnection();
            //定义sql
            String sql="delete from friendlist where user_id=? and friend_id=? ";
            //获取集装箱
            preparedStatement=connection.prepareStatement(sql);
            //给？赋值
            preparedStatement.setString(1,userId);
            preparedStatement.setString(2,friendId);
            //执行sql
            int result=preparedStatement.executeUpdate();


        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(resultSet,preparedStatement,connection);
        }
    }
}
